package com.zhhs.galaxyott.activity.moreMoviesActivity;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.KeyEvent;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.zhhs.galaxyott.R;
import com.zhhs.galaxyott.adapters.MoviesPagersAdapter;
import com.zhhs.galaxyott.utils.Logs;

public class MoreMoviesActivity extends FragmentActivity {

    private static String TAG = MoreMoviesActivity.class.getSimpleName();

    public static int pos;

    private static int index = 0;

    private MoreMoviesPresenter mPresenter;
    private MoreMoviesHolder mHolder;

    public static boolean detailFocus = false;
    public static boolean isFirstLoad = true;
    public static boolean isViewPager = false;
    public static int selectId = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        pos = getIntent().getIntExtra("type", 0);

        isFirstLoad = true;
        isList = true;
        detailFocus = false;
        listpos = pos;
        MoreMoviesHolder.lastSelect = pos;

        setContentView(R.layout.activity_more_movies);

        mPresenter = new MoreMoviesPresenter(this);
        mHolder = new MoreMoviesHolder(this);
        mPresenter.attachView(mHolder);

        mPresenter.loadSpecailData();

        mPresenter.loadData();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    private static int listpos = 1;
    private static boolean isList = true;

    /******************* 方案三 ********************************/

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (mPresenter.titleData == null || mPresenter.titleData.size() == 0) {
            return super.onKeyDown(keyCode, event);
        }

        switch (keyCode) {

        case KeyEvent.KEYCODE_DPAD_DOWN:

            if (((FrameLayout) mHolder.mListView.getChildAt(mPresenter.titleData.size() - 1)).getChildAt(0).hasFocus()) {
                return true;
            }
            // if (MoreMoviesHolder.imageLeft.hasFocus() ||
            // MoreMoviesHolder.imageRight.hasFocus()) {
            // Logs.logD("DEBUG_K", "THIS IS onkeydown--imageRight.hasFocus()");
            // return switchFragmentKeyDown(index, keyCode, event);
            // }

            break;

        case KeyEvent.KEYCODE_DPAD_UP:

            break;
        case KeyEvent.KEYCODE_DPAD_LEFT:
            if (MoreMoviesHolder.imageLeft.hasFocus()) {
                detailFocus = false;

            } else if (MoreMoviesHolder.imageRight.hasFocus()) {
                return super.onKeyDown(keyCode, event);
            }
            break;

        case KeyEvent.KEYCODE_DPAD_RIGHT:

            Logs.logD("DEBUG_K", "THIS IS onkeydown--KEYCODE_DPAD_RIGHT0");
            if (MoreMoviesHolder.imageLeft.hasFocus() || MoreMoviesHolder.imageRight.hasFocus()) {
                return super.onKeyDown(keyCode, event);
            }
            if (!detailFocus) {
                if (MoviesPagersAdapter.curpageItem > 0) {
                    isList = false;
                    ((TextView) ((FrameLayout) mHolder.mListView
                            .getChildAt(mHolder.mListView.getSelectedItemPosition())).getChildAt(0))
                            .setBackgroundResource(R.drawable.title_normal);
                } else {

                }

            }

            break;
        default:

            break;
        }
        Logs.logD("DEBUG_1", "THIS IS onKeyDown--test>0");

        index = selectId;
        // 防止右键移动2个位置
        if (switchFragmentKeyDown(index, keyCode, event))
            return true;
        Logs.logD("DEBUG_1", "THIS IS onKeyDown--test>2");

        if (!detailFocus) {
            isList = true;
            ((TextView) ((FrameLayout) mHolder.mListView.getChildAt(mHolder.mListView.getSelectedItemPosition()))
                    .getChildAt(0)).setBackgroundResource(R.drawable.title_focus);
            mHolder.mListView.requestFocus();
        }

        return super.onKeyDown(keyCode, event);
    }

    /**
     * 控制Title变化和列表数据变化
     */
    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if (event.getAction() == KeyEvent.ACTION_DOWN) {
            Logs.logD("DEBUG_K", "THIS IS dispatchKeyEvent:" + event.getKeyCode());

            if (mPresenter.titleData == null || mPresenter.titleData.size() == 0) {
                // if (event.getKeyCode() == KeyEvent.KEYCODE_BACK ||
                // event.getKeyCode() == 111) {
                // MoreMoviesActivity.this.finish();
                // }
                // return true;
                return super.dispatchKeyEvent(event);
            }

            switch (event.getKeyCode()) {
            case KeyEvent.KEYCODE_DPAD_DOWN:
                if (isList && !detailFocus) {
                    if (listpos < mPresenter.titleData.size() - 1) {
                        mHolder.mListView.setSelection(listpos + 1);
                        ((TextView) ((FrameLayout) mHolder.mListView.getChildAt(listpos)).getChildAt(0))
                                .setBackgroundResource(R.drawable.rect_transparent_bg);
                        ((TextView) ((FrameLayout) mHolder.mListView.getChildAt(listpos + 1)).getChildAt(0))
                                .setBackgroundResource(R.drawable.title_focus);
                        mHolder.mVpAdapter.initViews(listpos + 1);
                        mHolder.mViewPager.setCurrentItem(listpos + 1);
                        listpos = listpos + 1;
                        return true;
                    } else {
                        return true;
                    }

                }

                if (((FrameLayout) mHolder.mListView.getChildAt(mPresenter.titleData.size() - 1)).getChildAt(0)
                        .hasFocus()) {
                    return true;
                }

                if (detailFocus) {
                    if (MoreMoviesHolder.imageLeft.hasFocus() || MoreMoviesHolder.imageRight.hasFocus()) {
                        Logs.logD("DEBUG_K", "THIS IS onkeydown--imageRight.hasFocus()");
                        return switchFragmentKeyDown(index, event.getKeyCode(), event);
                    } else {
                        switchFragmentKeyDown(index, event.getKeyCode(), event);
                    }

                }

                break;
            case KeyEvent.KEYCODE_DPAD_UP:
                Logs.logD("DEBUG_1", "THIS IS dispatchKeyEvent--test>");
                if (isList && !detailFocus) {
                    if (listpos > 0) {
                        mHolder.mListView.setSelection(listpos - 1);
                        ((TextView) ((FrameLayout) mHolder.mListView.getChildAt(listpos)).getChildAt(0))
                                .setBackgroundResource(R.drawable.rect_transparent_bg);
                        ((TextView) ((FrameLayout) mHolder.mListView.getChildAt(listpos - 1)).getChildAt(0))
                                .setBackgroundResource(R.drawable.title_focus);
                        mHolder.mVpAdapter.initViews(listpos - 1);
                        mHolder.mViewPager.setCurrentItem(listpos - 1);
                        listpos = listpos - 1;
                        return true;
                    }

                }

                break;

            default:

                break;

            }

        }

        return super.dispatchKeyEvent(event);
    }

    /******************* end ********************************/

    private Boolean switchFragmentKeyDown(int index, int keyCode, KeyEvent event) {

        return MoviesPagersAdapter.onKeyDown(index, keyCode, event);

    }

    /************************************ 方案一 ***********************************/
    // @Override
    // public boolean onKeyDown(int keyCode, KeyEvent event) {
    //
    // switch (keyCode) {
    //
    // case KeyEvent.KEYCODE_DPAD_DOWN:
    //
    // if (((FrameLayout)
    // mHolder.mListView.getChildAt(mPresenter.titleData.size() -
    // 1)).getChildAt(0).hasFocus()) {
    // return true;
    // }
    // if (MoreMoviesHolder.imageLeft.hasFocus() ||
    // MoreMoviesHolder.imgeRight.hasFocus()) {
    // Logs.logD("DEBUG_K", "THIS IS onkeydown--imageRight.hasFocus()");
    // return switchFragmentKeyDown(index, keyCode, event);
    // }
    //
    // break;
    //
    // case KeyEvent.KEYCODE_DPAD_UP:
    //
    // break;
    // case KeyEvent.KEYCODE_DPAD_LEFT:
    // if (MoreMoviesHolder.imageLeft.hasFocus()) {
    // detailFocus = false;
    //
    // } else if (MoreMoviesHolder.imageRight.hasFocus()) {
    // return super.onKeyDown(keyCode, event);
    // }
    // break;
    //
    // case KeyEvent.KEYCODE_DPAD_RIGHT:
    //
    // Logs.logD("DEBUG_K", "THIS IS onkeydown--KEYCODE_DPAD_RIGHT0");
    // if (MoreMoviesHolder.imageLeft.hasFocus() ||
    // MoreMoviesHolder.imageRight.hasFocus()) {
    // return super.onKeyDown(keyCode, event);
    // }
    //
    // break;
    // default:
    //
    // break;
    // }
    // Logs.logD("DEBUG_1", "THIS IS onKeyDown--test>0");
    //
    // index = selectId;
    // // 防止右键移动2个位置
    // if (switchFragmentKeyDown(index, keyCode, event))
    // return true;
    // Logs.logD("DEBUG_1", "THIS IS onKeyDown--test>2");
    //
    // if (!detailFocus) {
    // isList = true;
    // ((TextView) ((FrameLayout)
    // mHolder.mListView.getChildAt(mHolder.mListView.getSelectedItemPosition()))
    // .getChildAt(0)).setBackgroundResource(R.drawable.titlelist_selector);
    // mHolder.mListView.requestFocus();
    // }
    //
    // return super.onKeyDown(keyCode, event);
    // }

    // @Override
    // public boolean dispatchKeyEvent(KeyEvent event) {
    // // TODO Auto-generated method stub
    // if (event.getAction() == KeyEvent.ACTION_DOWN) {
    //
    // if (mPresenter.titleData.size() == 0) {
    // return true;
    // }
    //
    // switch (event.getKeyCode()) {
    // case KeyEvent.KEYCODE_DPAD_DOWN:
    // if (isList) {
    // if (listpos < mPresenter.titleData.size() - 1) {
    // mHolder.mListView.setSelection(listpos + 1);
    // ((TextView) ((FrameLayout)
    // mHolder.mListView.getChildAt(listpos)).getChildAt(0))
    // .setBackgroundResource(R.drawable.rect_transparent_bg);
    // ((TextView) ((FrameLayout) mHolder.mListView.getChildAt(listpos +
    // 1)).getChildAt(0))
    // .setBackgroundResource(R.drawable.title_focus);
    // mHolder.mViewPager.setCurrentItem(listpos + 1);
    // listpos = listpos + 1;
    // return true;
    // } else {
    // return true;
    // }
    //
    // }
    //
    // if (((FrameLayout)
    // mHolder.mListView.getChildAt(mPresenter.titleData.size() -
    // 1)).getChildAt(0)
    // .hasFocus()) {
    // return true;
    // }
    // if (detailFocus == true
    // && !(MoreMoviesHolder.imageLeft.hasFocus() ||
    // MoreMoviesHolder.imageRight.hasFocus())) {
    // switchFragmentKeyDown(index, event.getKeyCode(), event);
    // }
    // break;
    // case KeyEvent.KEYCODE_DPAD_UP:
    // Logs.logD("DEBUG_1", "THIS IS dispatchKeyEvent--test>");
    // if (isList) {
    // if (listpos > 0) {
    // mHolder.mListView.setSelection(listpos - 1);
    // ((TextView) ((FrameLayout)
    // mHolder.mListView.getChildAt(listpos)).getChildAt(0))
    // .setBackgroundResource(R.drawable.rect_transparent_bg);
    // ((TextView) ((FrameLayout) mHolder.mListView.getChildAt(listpos -
    // 1)).getChildAt(0))
    // .setBackgroundResource(R.drawable.title_focus);
    // mHolder.mViewPager.setCurrentItem(listpos - 1);
    // listpos = listpos - 1;
    // return true;
    // }
    //
    // }
    //
    // if (detailFocus == true) {
    // switchFragmentKeyDown(index, event.getKeyCode(), event);
    // }
    // break;
    //
    // case KeyEvent.KEYCODE_DPAD_RIGHT:
    // Logs.logD("DEBUG_K", "THIS IS dispatchKeyEvent--KEYCODE_DPAD_RIGHT0" +
    // isList);
    // if (isList && MoviesPagersAdapter.curpageItem > 0) {
    // isList = false;
    // Logs.logD("DEBUG_K", "THIS IS dispatchKeyEvent--KEYCODE_DPAD_RIGHT");
    //
    // ((TextView) ((FrameLayout) mHolder.mListView
    // .getChildAt(mHolder.mListView.getSelectedItemPosition())).getChildAt(0))
    // .setBackgroundResource(R.drawable.title_normal);
    // return switchFragmentKeyDown(listpos, event.getKeyCode(), event);
    // }
    //
    // if (MoreMoviesHolder.imageLeft.hasFocus() ||
    // MoreMoviesHolder.imageRight.hasFocus()) {
    // return super.dispatchKeyEvent(event);
    // }
    //
    // break;
    //
    // case KeyEvent.KEYCODE_DPAD_LEFT:
    // if (MoreMoviesHolder.imageLeft.hasFocus()) {
    // detailFocus = false;
    //
    // } else if (MoreMoviesHolder.imageRight.hasFocus()) {
    // return super.dispatchKeyEvent(event);
    // }
    //
    // default:
    //
    // break;
    //
    // }
    //
    // }
    //
    // return super.dispatchKeyEvent(event);
    // }

    // private Boolean switchFragmentKeyDown(int index, int keyCode, KeyEvent
    // event) {
    //
    // return MoviesPagersAdapter.onKeyDown(index, keyCode, event);
    //
    // }

    /*********************** end ***************************************/

    /******************** 方案二 *************************************/
    // @Override
    // public boolean dispatchKeyEvent(KeyEvent event) {
    //
    // if (event.getAction() == KeyEvent.ACTION_DOWN) {
    // if (mPresenter.titleData.size() == 0) {
    // return true;
    // }
    //
    // switch (event.getKeyCode()) {
    // case KeyEvent.KEYCODE_DPAD_DOWN:
    // case KeyEvent.KEYCODE_DPAD_UP:
    // if (!detailFocus) {
    // MoreMoviesHolder.lastSelect =
    // mHolder.mListView.getSelectedItemPosition();
    // }
    //
    // break;
    // default:
    // break;
    // }
    // }
    //
    // return super.dispatchKeyEvent(event);
    // }
    /******************** end *************************************/

}
